package ph.com.gs3.formalistics.managers;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import android.os.AsyncTask;
import ph.com.gs3.formalistics.communicators.CommunicatorModule.CommunicatorException;
import ph.com.gs3.formalistics.communicators.DocumentCommunicatorModule;
import ph.com.gs3.formalistics.interfaces.CallbackCommand;
import ph.com.gs3.formalistics.model.dao.DocumentsDAO;
import ph.com.gs3.formalistics.model.valueobjects.application.APIResponse;
import ph.com.gs3.formalistics.model.valueobjects.application.APIResponse.APIInvalidResponseException;
import ph.com.gs3.formalistics.model.valueobjects.application.Document;
import ph.com.gs3.formalistics.model.valueobjects.application.DocumentStub;
import ph.com.gs3.formalistics.model.valueobjects.application.FormStub;
import ph.com.gs3.formalistics.utilities.FLLogger;

public class DocumentManager {

	public static final String TAG = DocumentManager.class.getSimpleName();

	private DocumentsDAO documentsDAO;
	private DocumentCommunicatorModule documentCommModule;

	public DocumentManager(DocumentsDAO documentsDAO, DocumentCommunicatorModule documentCommModule) {
		this.documentsDAO = documentsDAO;
		this.documentCommModule = documentCommModule;
	}

	public List<DocumentStub> getDocumentUpdates(List<FormStub> activeForms) {

		List<DocumentStub> stubList = new ArrayList<DocumentStub>();

		return stubList;

	}

	public void submitDocumentAction(String formId, String requestId, JSONObject requestData,
	        String action) {

	}

	public void submitDocumentAction(final Document document, final String action,
	        final CallbackCommand<SubmitDocumentActionResult> callback) {

		AsyncTask<Void, Void, SubmitDocumentActionResult> submitDocumentActionTask = new AsyncTask<Void, Void, DocumentManager.SubmitDocumentActionResult>() {

			@Override
			protected SubmitDocumentActionResult doInBackground(Void... params) {

				SubmitDocumentActionResult results = new SubmitDocumentActionResult();

				APIResponse response;
				try {
					response = documentCommModule.submitDocument(document.getFormWebId(),
					        document.getWebId(), document.getFields(), action);
					FLLogger.d(TAG, response.getResults());

					results.response = response;

				} catch (CommunicatorException e) {
					results.communicatorException = e;
				} catch (APIInvalidResponseException e) {
					results.invalidResponseException = e;
				}

				return results;
			}

			@Override
			protected void onPostExecute(SubmitDocumentActionResult result) {
				super.onPostExecute(result);

				if (callback != null) {
					callback.execute(result);
				}

			}

		};

		submitDocumentActionTask.execute();

	}

	// =================================================================================
	// Private/Result Classes

	public static class SubmitDocumentActionResult {

		public CommunicatorException communicatorException = null;
		public APIInvalidResponseException invalidResponseException = null;
		public APIResponse response = null;

	}

}
